package app.fugema.shop.helper;

import android.app.Activity;
import android.text.TextUtils;

import com.alipay.sdk.app.AuthTask;
import com.alipay.sdk.app.PayTask;
import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.utils.LogUtil;
import com.base.common.utils.ThreadManager;
import com.base.common.utils.UIUtils;

import java.util.Map;

import app.fugema.shop.R;

public class AlipayHelper {

    public static void pay(Activity activity, String orderInfo, AlipayCallBack alipayCallBack) {

        ThreadManager.getLongPool().execute(new Runnable() {
            @Override
            public void run() {
//                if (BuildConfig.isTest) {
//                    EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
//                }
                // 构造PayTask 对象
                PayTask alipay = new PayTask(activity);
                // 调用支付接口，获取支付结果
                Map<String, String> result = alipay.payV2(orderInfo, true);
                PayResult payResult = new PayResult(result);
                String resultStatus = payResult.getResultStatus();
                if (TextUtils.equals(resultStatus, "9000")) {
                    //通知提交页面支付成功
                    if (alipayCallBack != null) {
                        alipayCallBack.onSucc();
                    }
                } else {
                    // 判断resultStatus 为非"6001"则代表取消支付
                    if (TextUtils.equals(resultStatus, "6001")) {
                        UIUtils.showToastSafes("用户取消支付");
                    } else {
                        // 其他值就可以判断为支付失败 或者系统返回的错误
                        alipayCallBack.onFail();
                        UIUtils.showToastSafes(payResult.getMemo());
                    }
                }
            }
        });

    }


    public interface AlipayCallBack {
        void onSucc();

        void onFail();
    }

    /**
     *  支付宝 授权登录
     * @param activity
     * @param authInfo
     */
    public static void auth(Activity activity, String authInfo,AlipayAuthCallBack authCallBack)
    {
        ThreadManager.getLongPool().execute( ()->{

            // 构造AuthTask 对象
            AuthTask authTask = new AuthTask(activity);
            // 调用授权接口，获取授权结果
            Map<String, String> result = authTask.authV2(authInfo, true);

            LogUtil.e("Alipay_auth", JacksonUtils.transBean2Json(  result ));
            @SuppressWarnings("unchecked")
            AuthResult authResult  =  new AuthResult(result, true);
            String resultStatus = authResult.getResultStatus();
            // 判断resultStatus 为“9000”且result_code
            // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档

            if (TextUtils.equals(resultStatus, "9000")&& TextUtils.equals(authResult.getResultCode(), "200"))
            {
                // 获取alipay_open_id，调支付时作为参数extern_token 的value
                // 传入，则支付账户为该授权账户
                if (authCallBack != null)
                {
                    authCallBack.onSucc(authResult);
                }
            }
            else
            {
                // 其他状态值则为授权失败
                authCallBack.onFail( authResult.getMemo() );
            }

        } );
    }

    public interface AlipayAuthCallBack
    {
        void onSucc(AuthResult authResult);

        void onFail(String msg);
    }

}
